// 引入Sequelize模块
var bcrypt = require('bcryptjs');
const Sequelize = require('sequelize');

// 引入数据库实例
const db = require('../db');
// 定义model
const user = db.define('user', {
	objectId: {
		type: Sequelize.STRING(20),
		allowNull: false,
		unique: true,
		primaryKey: true
	},

	nickName: {
		type: Sequelize.TEXT,
		comment: "姓名",
		allowNull: false,
	},

	avatarUrl: {
		type: Sequelize.STRING(150),
		comment: "头像",
	},

	bgImageUrl: {
		type: Sequelize.STRING(150),
		comment: "背景图片",
	},

	signature: {
		type: Sequelize.STRING(200),
		comment: "签名",
	},

	address: {
		type: Sequelize.TEXT,
		comment: "地址",
	},

	sex: {
		type: Sequelize.INTEGER,
		comment: "性别,0未知，1男生，2女生",
		defaultValue: 0,
	},

	mobilePhoneNumber: {
		type: Sequelize.BIGINT(11),
		comment: "联系方式1",
	},

	isDisabled: {
		type: Sequelize.BOOLEAN,
		comment: "是否启用",
		defaultValue: 1,
	},

	isCreater: {
		type: Sequelize.INTEGER,
		comment: "创作者状态。-1：未申请，0：已申请，1：是",
		defaultValue: -1,
	},

	wcMiniId: {
		type: Sequelize.STRING,
		comment: "微信小程序Id",
		unique: true,
	},


	storeMoney: {
		type: Sequelize.DECIMAL(18, 3),
		comment: "可提现余额",
		defaultValue: 0,
		get() {
			return Number(this.getDataValue('storeMoney'))
		}
	},

	vipTime: {
		type: Sequelize.BIGINT,
		comment: "会员时间",
		get() {
			const rawValue = this.getDataValue('vipTime');
			if (rawValue && rawValue >= 0) {
				if (rawValue == 4102416000000) {
					return "永久";
				} else if (rawValue <= new Date().getTime()) {
					return 0;
				} else {
					return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
				}
			}
		}
	},

	createdAt: {
		type: Sequelize.DATE,
		get() {
			const rawValue = this.getDataValue('createdAt');
			return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
		}
	},

	updatedAt: {
		type: Sequelize.DATE,
	},
}, {
	// 是否支持驼峰
	underscored: false,
	// MySQL数据库表名
	tableName: 'user',
});

(async () => {
	/*await user.sync({
		alter: true,
	});
	console.log("User字段同步成功！");*/
})();


module.exports = {
	user
};
